home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / tde3.zip / PORT.C < prev    next >
C/C++ Source or Header  |  1993-06-05  |  9KB  |  269 lines

  1. /*
  2.  * Now that I own both MSC 7.0 and BC 3.1, lets rearrange stuff so both
  3.  * compilers can compile TDE.  Several implementation specific functions
  4.  * needed for both compilers were gathered into this file.
  5.  *
  6.  * Incidentally, there is difference between a NULL line pointer and
  7.  * a pointer to a line that contains no characters.  For example, calling
  8.  *
  9.  *       line = malloc( 0 );
  10.  *
  11.  *                   or, more precisely in TDE:
  12.  *
  13.  *       line = _fmalloc( 0 );
  14.  *       line = farmalloc( 0 );
  15.  *
  16.  * will return a valid pointer to an item of 0 length in some compilers
  17.  * and a NULL pointer in other compilers.  malloc( 0 ) will return a valid
  18.  * pointer to an object of zero length in MSC.  malloc( 0 ) will return a
  19.  * NULL pointer in BC.  The problem with returning a NULL pointer for
  20.  * malloc( 0 ) is that it's a little harder to tell if the heap is out of
  21.  * memory or if we have a valid NULL pointer.  On the other hand, the good
  22.  * part about returning a NULL pointer for malloc( 0 ) is that extra space
  23.  * is not wasted for an object of 0 length.  In TDE, we will test for 0
  24.  * before calling my_malloc( ) and set an ERROR code if out of memory.
  25.  *
  26.  * Although many PC C compilers have findfirst and findnext functions for
  27.  * finding files, let's write our own to keep a closer watch on
  28.  * critical errors.
  29.  *
  30.  *
  31.  * New editor name:  TDE, the Thomson-Davis Editor.
  32.  * Author:           Frank Davis
  33.  * Date:             June 5, 1991, version 1.0
  34.  * Date:             July 29, 1991, version 1.1
  35.  * Date:             October 5, 1991, version 1.2
  36.  * Date:             January 20, 1992, version 1.3
  37.  * Date:             February 17, 1992, version 1.4
  38.  * Date:             April 1, 1992, version 1.5
  39.  * Date:             June 5, 1992, version 2.0
  40.  * Date:             October 31, 1992, version 2.1
  41.  * Date:             April 1, 1993, version 2.2
  42.  * Date:             June 5, 1993, version 3.0
  43.  *
  44.  * This code is released into the public domain, Frank Davis.
  45.  * You may use and distribute it freely.
  46.  */
  47.  
  48. #include "tdestr.h"
  49. #include "common.h"
  50. #include "tdefunc.h"
  51. #include "define.h"
  52.  
  53.  
  54. /*
  55.  * Name:    my_malloc
  56.  * Purpose: malloc from the far heap
  57.  * Date:    April 1, 1993
  58.  * Passed:  mem:  pointer to memory to free in far heap
  59.  *          rc:   pointer to return code
  60.  * Notes:   set the return code only if an ERROR occured with malloc.
  61.  *           returning a NULL pointer is not neccessarily an ERROR.
  62.  */
  63. void far * my_malloc( size_t size, int *rc )
  64. {
  65. void far *mem;
  66.  
  67.    assert( size < MAX_LINE_LENGTH );
  68.  
  69.    if (size == 0)
  70.  
  71.       /*
  72.        * if 0 bytes are requested, return NULL
  73.        */
  74.       mem = NULL;
  75.    else {
  76.  
  77. #if defined( __MSC__ )
  78.       mem = _fmalloc( size );
  79. #else
  80.       mem = farmalloc( size );
  81. #endif
  82.  
  83.       /*
  84.        * if malloc failed, return NULL and an ERROR.
  85.        */
  86.       if (mem == NULL)
  87.          *rc = ERROR;
  88.    }
  89.    return( mem );
  90. }
  91.  
  92.  
  93. /*
  94.  * Name:    my_free
  95.  * Purpose: free memory from the far heap
  96.  * Date:    April 1, 1993
  97.  * Passed:  mem:  pointer to memory to free in far heap
  98.  */
  99. void my_free( void far *mem )
  100. {
  101.    assert( mem != NULL );
  102.  
  103. #if defined( __MSC__ )
  104.    _ffree( mem );
  105. #else
  106.    farfree( mem );
  107. #endif
  108. }
  109.  
  110.  
  111. /*
  112.  * Name:    my_findfirst
  113.  * Purpose: find the first file matching a pattern using DOS interrupt
  114.  * Date:    January 6, 1992
  115.  * Passed:  dta:    disk transfer address
  116.  *          path:   path to search for files
  117.  *          f_attr: attributes of files to search for
  118.  * Notes:   return codes for my_findfirst:
  119.  *             0  no error
  120.  *             2  file is invalid or does not exist
  121.  *             3  path is invalid or does not exist
  122.  *            18  no matching directory entry was found
  123.  *            -1  check the critical error flag for critical errors
  124.  */
  125. int  my_findfirst( DTA far *dta, char far *path, int f_attr )
  126. {
  127. void far *old_dta;
  128. void far *new_dta;
  129. int  rc;
  130.  
  131.    new_dta = (void far *)dta;
  132.  
  133.    ASSEMBLE {
  134.  
  135. /*
  136. ; save the old dta
  137. */
  138.         mov     ah, 0x2f                /* DOS get dta */
  139.         int     0x21                    /* DOS interrupt */
  140.         mov     WORD PTR old_dta, bx    /* save OFFSET of old DTA */
  141.         mov     ax, es
  142.         mov     WORD PTR old_dta+2, ax  /* save SEGMENT of old DTA */
  143.  
  144. /*
  145. ; set the new dta
  146. */
  147.         push    ds                      /* save ds */
  148.         mov     dx, WORD PTR new_dta    /* get OFFSET of new dta */
  149.         mov     ax, WORD PTR new_dta+2  /* get SEGMENT of new dta */
  150.         mov     ds, ax                  /* put it in ds */
  151.         mov     ah, 0x1a                /* DOS set dta */
  152.         int     0x21                    /* DOS interrupt */
  153.         pop     ds                      /* get back ds */
  154.  
  155. /*
  156. ; find first matching file
  157. */
  158.         push    ds                      /* save ds */
  159.         mov     cx, WORD PTR f_attr     /* file attributes to search for */
  160.         mov     dx, WORD PTR path       /* get OFFSET of path */
  161.         mov     ax, WORD PTR path+2     /* get SEGMENT of path */
  162.         mov     ds, ax                  /* put it in ds */
  163.         mov     ah, 0x4e                /* DOS find first file */
  164.         int     0x21                    /* DOS interrupt */
  165.         pop     ds                      /* get back ds */
  166.  
  167. /*
  168. ; save the return code
  169. */
  170.         jc      an_error                /* carry is set if an error occured */
  171.         xor     ax, ax                  /* zero out ax, return OK if no error */
  172.    }
  173. an_error:
  174.  
  175.    ASSEMBLE {
  176.         mov     WORD PTR rc, ax         /* save the return code */
  177.  
  178. /*
  179. ; get back old dta
  180. */
  181.         push    ds                      /* save ds */
  182.         mov     dx, WORD PTR old_dta    /* get OFFSET of old dta */
  183.         mov     ax, WORD PTR old_dta+2  /* get SEGMENT of old dta */
  184.         mov     ds, ax                  /* put it in ds */
  185.         mov     ah, 0x1a                /* DOS set dta */
  186.         int     0x21                    /* DOS interrupt */
  187.         pop     ds                      /* get back ds */
  188.    }
  189.    if (ceh.flag == ERROR)
  190.       rc = ERROR;
  191.    return( rc );
  192. }
  193.  
  194.  
  195. /*
  196.  * Name:    my_findnext
  197.  * Purpose: find the next file matching a pattern using DOS interrupt
  198.  * Date:    January 6, 1992
  199.  * Passed:  dta:  disk transfer address
  200.  * Notes:   my_findfirst() MUST be called before calling this function.
  201.  *          return codes for my_findnext (see DOS tech ref manuals):
  202.  *             0  no error
  203.  *             2  path is invalid or does not exist
  204.  *            18  no matching directory entry was found
  205.  *            -1  check the critical error flag for critical errors
  206.  */
  207. int  my_findnext( DTA far *dta )
  208. {
  209. void far *old_dta;
  210. void far *new_dta;
  211. int  rc;
  212.  
  213.    new_dta = (void far *)dta;
  214.  
  215.    ASSEMBLE {
  216.  
  217. /*
  218. ; save the old dta
  219. */
  220.         mov     ah, 0x2f                /* DOS get dta */
  221.         int     0x21                    /* DOS interrupt */
  222.         mov     WORD PTR old_dta, bx    /* save OFFSET of old DTA */
  223.         mov     ax, es
  224.         mov     WORD PTR old_dta+2, ax  /* save SEGMENT of old DTA */
  225.  
  226. /*
  227. ; set the new dta
  228. */
  229.         push    ds                      /* save ds */
  230.         mov     dx, WORD PTR new_dta    /* get OFFSET of new dta */
  231.         mov     ax, WORD PTR new_dta+2  /* get SEGMENT of new dta */
  232.         mov     ds, ax                  /* put it in ds */
  233.         mov     ah, 0x1a                /* DOS set dta */
  234.         int     0x21                    /* DOS interrupt */
  235.         pop     ds                      /* get back ds */
  236.  
  237. /*
  238. ; find next matching file
  239. */
  240.         mov     ah, 0x4f                /* DOS find first file */
  241.         int     0x21                    /* DOS interrupt */
  242.  
  243. /*
  244. ; save the return code
  245. */
  246.         jc      an_error                /* carry is set if an error occured */
  247.         xor     ax, ax                  /* zero out ax, return OK if no error */
  248.    }
  249. an_error:
  250.  
  251.    ASSEMBLE {
  252.         mov     WORD PTR rc, ax         /* save the return code */
  253.  
  254. /*
  255. ; get back old dta
  256. */
  257.         push    ds                      /* save ds */
  258.         mov     dx, WORD PTR old_dta    /* get OFFSET of old dta */
  259.         mov     ax, WORD PTR old_dta+2  /* get SEGMENT of old dta */
  260.         mov     ds, ax                  /* put it in ds */
  261.         mov     ah, 0x1a                /* DOS set dta */
  262.         int     0x21                    /* DOS interrupt */
  263.         pop     ds                      /* get back ds */
  264.    }
  265.    if (ceh.flag == ERROR)
  266.       rc = ERROR;
  267.    return( rc );
  268. }
  269.